﻿/**
 * Create in 2012-4-3.
 */
package kit4j.tests.jdk.awt;

import java.awt.GridLayout;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;

import kit4j.tests.util.AWTFactory;


/**
 * 
 * @author MiniKnife
 *
 */
public class TestJProcessBar {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = getFrame();
		JProgressBar bar = getProgressBar();
		frame.add(bar);
		frame.setVisible(true);
		
		runProgressBar(bar);
	}
	
	static JFrame getFrame() {
		JFrame frame = AWTFactory.createJFrame("Test JProgressBar", 400, 50);
		frame.setLayout(new GridLayout(1, 1));
		
		return frame;
	}
	
	static JProgressBar getProgressBar() {
		JProgressBar bar = new JProgressBar(SwingConstants.HORIZONTAL);
		bar.setBorderPainted(false);
		
		return bar;
	}
	
	static void runProgressBar(final JProgressBar bar) {
		while(bar.getValue() < bar.getMaximum()) {
			bar.setValue(bar.getValue() + 1);
			
			System.out.println(bar.getValue());
			
			try {
				TimeUnit.MILLISECONDS.sleep(30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		bar.setValue(0);
		bar.setIndeterminate(true);
		while(bar.getValue() < bar.getMaximum()) {
			bar.setValue(bar.getValue() + 1);
			
			System.out.println(bar.getValue());
			
			try {
				TimeUnit.MILLISECONDS.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		bar.setMaximum(800);
		bar.setIndeterminate(false);
		while(bar.getValue() < bar.getMaximum()) {
			bar.setValue(bar.getValue() + 15);
			
			System.out.println(bar.getValue());
			
			try {
				TimeUnit.MILLISECONDS.sleep(30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
